package com.chongqing.shiro.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class PasswordHelper {

    private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    private static String algorithmName = "md5";
    private static int hashIterators = 2;
    private static String Credentials;

    public static String getCredentials() {
        return Credentials;
    }

    public static void setCredentials(String userName, String salt) {
        Credentials = userName + "nbclass.com" + salt;
    }


    public static String getSalt() {
        return randomNumberGenerator.nextBytes().toHex();
    }

    public static String encryptPassword(String password) {
        String newPassword = new SimpleHash(algorithmName, password, ByteSource.Util.bytes(getCredentials()), hashIterators).toHex();
        return newPassword;
    }

    public static String getPassword(String password) {
        return new SimpleHash(algorithmName, password, ByteSource.Util.bytes(getCredentials()), hashIterators).toHex();
    }

    public static void main(String[] args) {

        String salt = getSalt();
        System.out.println(salt);
        setCredentials("root", salt);
        System.out.println(encryptPassword("windows"));

        setCredentials("root", salt);
        System.out.println(getPassword("windows"));


    }
}
